import { useRequest } from 'umi';
import {
  queryAreaListAPI,
  queryUserListAPI,
  addUserAPI,
  queryUserInfoAPI,
  editUserAPI,
  deleteUserAPI,
} from './services';
import { createModel } from 'hox';
import { useState } from 'react';
import { DataNode } from 'antd/lib/tree';
import { AreaListReturnProps } from './API';
import { queryCardList } from '../Card/services';
type AreaListProps = DataNode & { code?: string };
interface HoxModelProps {
  areaList: AreaListProps[];
  selectedKeys: string[];
  selectedRowKey: Partial<AreaListProps>;
  currentCode: string;
  formAreaList: AreaListReturnProps[];
  userFormAreaList: AreaListReturnProps[];
  info: { id?: number; [key: string]: any };
}
function useHoxModel() {
  const [state, setState] = useState<HoxModelProps>({
    areaList: [],
    selectedKeys: [],
    selectedRowKey: {},
    currentCode: '',
    info: {},
    formAreaList: [],
    userFormAreaList: [],
  });
  const dispatch = (payload: Partial<HoxModelProps>) => {
    setState((pre) => ({ ...pre, ...payload }));
  };

  //获取卡片接口
  const { run: getAreaList, data: currentAreaList } = useRequest(queryAreaListAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取卡片接口
  const { run: getFormAreaList } = useRequest(queryAreaListAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //添加用户接口
  const { run: addUser } = useRequest(addUserAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取卡片接口
  const { run: getCardList, data: blankCardList } = useRequest(queryCardList, {
    manual: true,
  });
  //删除用户接口
  const { run: deleteUser } = useRequest(deleteUserAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //修改用户接口
  const { run: editUser } = useRequest(editUserAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取用户详情接口
  const { run: getUserInfo, data: userInfo, loading: userInfoLoading } = useRequest(
    queryUserInfoAPI,
    {
      manual: true,
    },
  );
  //获取用户列表接口
  const { run: getUserList, data: userList } = useRequest(queryUserListAPI, {
    manual: true,
  });
  //第一次获取区域
  const firstGetAreaList = async () => {
    let currentArea = await getAreaList();
    if (currentArea && currentArea.result === 0) {
      //数据格式转换
      const areaList = currentArea.data.map((item: any) => {
        return {
          key: item.id.toString(),
          title: item.name,
          isLeaf: item.isRegion,
          code: item.code,
        };
      });
      //默认选中第一个
      let item = areaList[0];

      dispatch({
        areaList,
        selectedRowKey: item,
        selectedKeys: [item.key],
        currentCode: item.code,
      });
    }
  };
  return {
    ...state,
    dispatch,
    addUser,
    getAreaList,
    editUser,
    userList,
    getCardList,
    blankCardList,
    getFormAreaList,
    getUserInfo,
    userInfo,
    deleteUser,
    userInfoLoading,
    currentAreaList: currentAreaList?.data || [],
    firstGetAreaList,
    getUserList,
  };
}
export default createModel(useHoxModel);
